package com.shire.shire_interface.controller.invite;

import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.common.utils.DateUtil;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.invite.WxInviteDayRecord;
import com.shire.shire_interface.model.invite.WxInviteSetting;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.account.WxAccountService;
import com.shire.shire_interface.service.invite.WxInviteSettingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

//@Api(tags = "加好友 添加好友/添加群好友")
@RestController
@RequestMapping("/invite")
public class WxInviteSettingController extends BaseController {

    @Resource
    private WxInviteSettingService wxInviteSettingService;

    @Resource
    private WxAccountService wxAccountService;

//    @ApiOperation(value = "添加设置 csid, wxaccountid")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", dataType = "WxInviteSetting", name = "wxInviteSetting")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping(value = "/insertInviteSetting")
    @ResponseBody
    public Response insertInviteSetting(@RequestBody WxInviteSetting wxInviteSetting, HttpSession session) {
        Response response = new Response();
        try {
            if (!checkLoginUserByCsId(session, wxInviteSetting.getCsid())){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            String sleepstart = wxInviteSetting.getSleepstart();
            String sleepend = wxInviteSetting.getSleepend();

            wxInviteSetting.setSleepstart(DateUtil.getDate()+" "+sleepstart);
            wxInviteSetting.setSleepend(DateUtil.getDate()+" "+sleepend);
            wxInviteSettingService.insertInviteSetting(wxInviteSetting);
            response.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            response.error("失败");
        }
        return response;
    }

//    @ApiOperation(value = "查询设置 csid, wxaccountid")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", dataType = "WxInviteSetting", name = "wxInviteSetting")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping(value = "/getInviteSetting")
    @ResponseBody
    public Response getInviteSetting(@RequestBody WxInviteSetting wxInviteSetting, HttpSession session) {
        Response response = new Response();

        try {
            if (!checkLoginUser(session )){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
//            WxInviteSettingExample example = new WxInviteSettingExample();
//            example.createCriteria().andCsidEqualTo(wxInviteSetting.getCsid()).andWxaccountidEqualTo(wxInviteSetting.getWxaccountid());
            WxInviteSetting inviteSetting = wxInviteSettingService.getInviteSetting(wxInviteSetting.getCsid(), wxInviteSetting.getWxaccountid());

            if (inviteSetting == null ) {
                inviteSetting = wxInviteSettingService.getDefaultSetting();
                inviteSetting.setCsid(wxInviteSetting.getCsid());
                inviteSetting.setWxaccountid(wxInviteSetting.getWxaccountid());
            }

            WxAccount account = wxAccountService.getWxAccountByWxId(inviteSetting.getWxaccountid());
            WxInviteDayRecord dayRecord = wxInviteSettingService.getInviteDayRecordForAccount(getCurrentUser(session).getId(),account.getId(),DateUtil.Date2String(new Date()));
            if (dayRecord == null)
                inviteSetting.setSendCount(0);
            else {
                inviteSetting.setSendCount(dayRecord.getInvitecount());
            }

            response.success("成功");
            response.setData(inviteSetting);
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "更新设置 csid, wxaccountid")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", dataType = "WxInviteSetting", name = "wxInviteSetting")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping(value = "/updateInviteSetting")
    @ResponseBody
    public Response updateInviteSetting(@RequestBody WxInviteSetting wxInviteSetting, HttpSession session) {
        Response response = new Response();

        try {
            if (!checkLoginUserByCsId(session, wxInviteSetting.getCsid())){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            if (wxInviteSetting.getId() == null){
                wxInviteSetting.setCreatetime(new Date());
                wxInviteSettingService.insertInviteSetting(wxInviteSetting);
            }else {
                wxInviteSetting.setUpdatetime(new Date());
                wxInviteSettingService.updateInviteSetting(wxInviteSetting);
            }
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
